import { pathOr } from 'ramda';
import { warnEventId } from '../../../../common/warnId';

const severityMap = {
	0: { key: 'UNKNOWN', zh_CN: '未知', level: 1 },
	1: { key: 'INFO', zh_CN: '信息', level: 1 },
	2: { key: 'LOW', zh_CN: '低危', level: 1 },
	3: { key: 'MEDIUM', zh_CN: '中危', level: 2 },
	4: { key: 'HIGH', zh_CN: '高危', level: 3 },
	5: { key: 'CRITICAL', zh_CN: '严重', level: 3 },
};

export default function vul({
	taskSettings,
	response: { statistics, info, vuln: results },
}) {
	const pluginCategories = taskSettings?.alert?.vulType || [];
	return {
		addition: { statistics, info },
		warnings: (results || [])
			.filter(x => {
				if (pluginCategories.length === 0) return x;
				const category = pathOr('', ['vuln_type', 0, 'en_us'], x);
				return pluginCategories.includes(category);
			})
			.map(x => ({
				id: warnEventId({
					taskType: 'vul',
					id: x.source_id,
					affects: x.url || x.affect,
				}),
				level: severityMap[x.severity]?.level,
				levelName: severityMap[x.severity]?.zh_CN,
				title: x.name.zh_cn,
				category: pathOr('', ['vuln_type', 0, 'zh_cn'], x),
				affects: x.url || x.affect,
				detailText: x.extra_detail,
				recommendation: x.recommendation.zh_cn,
				impact: x.impact.zh_cn,
				description: x.description.zh_cn,
				reference: x.reference,
				cnvd: x.cnvd,
				foundAt: x.found_at,
				traffics: x.traffics,
				payload: x.payload,
			})),
	};
}
